package com.ujs.edu.medicine.dao;

import com.ujs.edu.medicine.dataobject.YunjiayiMessage;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
@Mapper
public interface YunjiayiMessageMapper {
    @Delete({
        "delete from yunjiayi_message",
        "where message_id = #{messageId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer messageId);

    /*通过UID删除*/
    @Delete({
            "delete from yunjiayi_message",
            "where fromuserid = #{formuserId,jdbcType=INTEGER}"
    })
    int deleteByUID(Integer formuserId);

    @Insert({
        "insert into yunjiayi_message (message_id, content, ",
        "fromuserid, create_time, ",
        "touserid, delete_time, ",
        "prescriptionId, messagestatus, ",
        "isFile, chatId)",
        "values (#{messageId,jdbcType=INTEGER}, #{content,jdbcType=VARCHAR}, ",
        "#{fromuserid,jdbcType=INTEGER}, now(), ",
        "#{touserid,jdbcType=INTEGER}, now(), ",
        "#{prescriptionid,jdbcType=INTEGER}, #{messagestatus,jdbcType=VARCHAR}, ",
        "#{isfile,jdbcType=INTEGER}, #{chatid,jdbcType=INTEGER})"
    })
    int insert(YunjiayiMessage record);

    @Select({
        "select",
        "message_id, content, fromuserid, create_time, touserid, delete_time, prescriptionId, ",
        "messagestatus, isFile, chatId",
        "from yunjiayi_message",
        "where message_id = #{messageId,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="message_id", property="messageId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="fromuserid", property="fromuserid", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="touserid", property="touserid", jdbcType=JdbcType.INTEGER),
        @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="prescriptionId", property="prescriptionid", jdbcType=JdbcType.INTEGER),
        @Result(column="messagestatus", property="messagestatus", jdbcType=JdbcType.VARCHAR),
        @Result(column="isFile", property="isfile", jdbcType=JdbcType.INTEGER),
        @Result(column="chatId", property="chatid", jdbcType=JdbcType.INTEGER)
    })
    YunjiayiMessage selectByPrimaryKey(Integer messageId);

    @Select({
        "select",
        "message_id, content, fromuserid, create_time, touserid, delete_time, prescriptionId, ",
        "messagestatus, isFile, chatId",
        "from yunjiayi_message"
    })
    @Results(id = "tableChatMesMap",value = {
        @Result(column="message_id", property="messageId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="fromuserid", property="fromuserid", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="touserid", property="touserid", jdbcType=JdbcType.INTEGER),
        @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="prescriptionId", property="prescriptionid", jdbcType=JdbcType.INTEGER),
        @Result(column="messagestatus", property="messagestatus", jdbcType=JdbcType.VARCHAR),
        @Result(column="isFile", property="isfile", jdbcType=JdbcType.INTEGER),
        @Result(column="chatId", property="chatid", jdbcType=JdbcType.INTEGER)
    })
    List<YunjiayiMessage> selectAll();


    /*查找聊天消息*/
    @Select("select * from yunjiayi_message where content like '%${content}%' and (fromuserid=#{fromuserid} or fromuserid=#{touserid})")
    @ResultMap("tableChatMesMap")
    List<YunjiayiMessage> selectMessageLike(@Param("fromuserid") Integer fromuserid,@Param("touserid")Integer touserid,@Param("content")String content);

    /*查看是否已经有了聊天对话*/
    @Select("select * from yunjiayi_message where fromuserid=#{fromuId} and touserid=#{touId}")
    List<YunjiayiMessage> getMessage(@Param("fromuId")Integer fromuId,@Param("touId")Integer touId);


    /*统计未读信息，通过to和from以及status*/
    @Select("select count(*) from yunjiayi_message where chatId=#{chatId} and messagestatus=#{messagestatus}")
    Integer countunreadmesBystatus(@Param("chatId")Integer chatId,@Param("messagestatus") String messagestatus);


    /*查询两个用户的所有聊天信息*/
    @Select("select * from yunjiayi_message where fromuserid=#{fromuserid} or fromuserid=#{touserid}")
    @ResultMap("tableChatMesMap")
    List<YunjiayiMessage> selectByToAndFromUSer(@Param("fromuserid")Integer fId,@Param("touserid")Integer toId);

    /*查询两个用户的所有聊天信息*/
    @Select("select * from yunjiayi_message where fromuserid=#{fromuserid} or touserid=#{fromuserid}")
    @ResultMap("tableChatMesMap")
    List<YunjiayiMessage> selectByUSer(@Param("fromuserid")Integer fId);

    /*通过chatID查到所有的该用户的所有的信息*/
    @Select("select * from yunjiayi_message where chatId=#{chatId}")
    List<YunjiayiMessage> selectByChatId(@Param("chatId")Integer chatId);

    /*查询最大的日期*/
    @Select("select max(message_id) content from yunjiayi_message where chatId=#{chatId}")
    Integer selectMaxDate(@Param("chatId") Integer chatId);

    /*通过chatID和时间查到最后一次对话的信息*/
    @Select("select content from yunjiayi_message where message_id=#{message_id}")
    String selectByChatIdAndLast(@Param("message_id") Integer message_id);


    /*删除两个用户所有的信息*/
    @Delete("delete from yunjiayi_message where fromuserid=#{fromuserid} or fromuserid=#{touserid}")
    int deleteByuIdAndToId(@Param("fromuserid")Integer fromuserid);

    @Update({
        "update yunjiayi_message",
        "set content = #{content,jdbcType=VARCHAR},",
          "fromuserid = #{fromuserid,jdbcType=INTEGER},",
          "create_time = now(),",
          "touserid = #{touserid,jdbcType=INTEGER},",
          "prescriptionId = #{prescriptionid,jdbcType=INTEGER},",
          "messagestatus = #{messagestatus,jdbcType=VARCHAR},",
          "isFile = #{isfile,jdbcType=INTEGER},",
          "chatId = #{chatid,jdbcType=INTEGER}",
        "where message_id = #{messageId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(YunjiayiMessage record);
}